﻿using Common.Attributes;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace QGZX.Logic.Cwy
{
    public class InformationController : Controller
    {
       
        //------------------对cwy_general基本信息表的增删查改---------------//
        #region 加载乘务员基本信息视图--ActionResult Index()
        /// <summary>
        /// 乘务员基本信息视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        } 
        #endregion

        #region 获取乘务员cwy_general表格数据 +GetGeneralData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取乘务员cwy_general表格数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetGeneralData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_generalBLL.GetPagedList(pageIndex, pageSize, x => x.GeneralId != null, m => m.Year).Select(x => x.ToPOCO());
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改乘务员概要信息表的 window -- ActionResult EditGeneral（）
        /// <summary>
        /// 加载修改乘务员概要信息表的 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditGeneral(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_generalBLL.GetListBy(x => x.GeneralId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改乘务员概要信息表的 window EditGeneral(MODEL.qgzx_cwy_general model)
        /// <summary>
        ///post修改乘务员概要信息表的 EditGeneral(MODEL.qgzx_company model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditGeneral(MODEL.qgzx_cwy_general model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_generalBLL.Modify(model, "Year", "Background", "Introduce", "Contacts", "TimeArrange", "Volunteers", "WageScale", "Trafficsituation", "TotalPassengers", "IsOpen", "BeginTime", "endTime");
            if (res > 0)
            {
                return Redirect("/cwy/Information/Index");
            }
            return Redirect("/cwy/Information/Index");
        }
        #endregion

        #region 新增乘务员概括的window + AddGeneral()
        /// <summary>
        /// 新增乘务员概括的 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddGeneral()
        {
            return PartialView("EditGeneral");
        }
        #endregion

        #region 新增乘务员概括的 html代码 + AddGeneral(MODEL.qgzx_cwy_general model)
        /// <summary>
        /// 新增乘务员概括的 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddGeneral(MODEL.qgzx_cwy_general model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_generalBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/cwy/Information/Index?ok");
            }
            return Redirect("/cwy/Information/Index?err");
        }
        #endregion

        #region 删除乘务员概要信息 + DelGeneral()
        /// <summary>
        /// 删除乘务员概要信息 + DelGeneral()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelGeneral()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_cwy_generalBLL.DelBy(x => x.GeneralId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了,您现在删除的单位正在被使用" + ex, null, "");
            }
        }
        #endregion

        #region 搜索方法 --ActionResult SearchGeneral()
        /// <summary>
        /// 多条件搜索
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SearchGeneral()
        {
            string Year = Request.QueryString["Year"];
            string Contacts = Request.QueryString["Contacts"];
            string  Volunteers = Request.QueryString["Volunteers"];
            string WageScale = Request.QueryString["WageScale"];
            string Trafficsituation = Request.QueryString["Trafficsituation"];
            string TotalPassengers = Request.QueryString["TotalPassengers"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！

            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_generalBLL.GetListBy(x => x.GeneralId != null).ToList().Select(x => x.ToPOCO());
            if (Year != "")
            {
                list = list.Where(x => x.Year.ToString().Contains(Year)).ToList();
            }
            if (Contacts != "")
            {
                list = list.Where(x => x.Contacts.Contains(Contacts)).ToList();
            }
            if (Volunteers != null)
            {
                list = list.Where(x => x.Volunteers.ToString().Contains(Volunteers)).ToList();
            }
            if (WageScale != "")
            {
                list = list.Where(x => x.WageScale.ToString().Contains(WageScale)).ToList();
            }
            if (Trafficsituation != "")
            {
                list = list.Where(x => x.Trafficsituation.Contains(Trafficsituation)).ToList();
            }
            if (TotalPassengers != "")
            {
                list = list.Where(x => x.TotalPassengers.Contains(TotalPassengers)).ToList();
            }

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        //-----------------对表cwy_fleet车队信息表的增删改查-------------//
        #region 获取乘务员cwy_fleet表格数据 +GetFleetData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取乘务员cwy_fleet表格数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetFleetData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_fleetBLL.GetPagedList(pageIndex, pageSize, x =>x.FleetId != null && x.General != null, m => m.FleetId).Select(x => x.ToPOCO());
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改乘务员车队cwy_fleet信息表的 window -- ActionResult EditFleet（）
        /// <summary>
        /// 加载修改乘务员车队cwy_fleet信息表的 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditFleet(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_fleetBLL.GetListBy(x => x.FleetId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改乘务员车队cwy_fleet信息表的 EditFleet(MODEL.qgzx_cwy_fleet model)
        /// <summary>
        ///post修改乘务员车队cwy_fleet信息表的 EditFleet(MODEL.qgzx_cwy_fleet model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditFleet(MODEL.qgzx_cwy_fleet model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_fleetBLL.Modify(model, "Introduce", "Stations", "WorkPlace", "OfficePhone", "Leader","StudentName","StudentNum","Volunteers", "Passengers", "GroupNums", "StartTime", "EndTime");
            if (res > 0)
            {
                return Redirect("/cwy/Information/GetFleetData");
            }
            return Redirect("/cwy/Information/GetFleetData");
        }
        #endregion

        #region 新增乘务员车队信息cwy_fleet的window + AddFleet()
        /// <summary>
        /// 新增乘务员车队信息cwy_fleet的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddFleet(int id)
        {
            MODEL.ViewModel.CwyFleet fleet = new MODEL.ViewModel.CwyFleet();
            fleet.General = id;
            return PartialView("EditFleet",fleet);
        }
        #endregion

        #region 新增乘务员车队信息cwy_fleet的 html代码 + AddFleet(MODEL.qgzx_cwy_fleet model)
        /// <summary>
        /// 新增乘务员车队信息cwy_fleet的 html代码 + AddFleet(MODEL.qgzx_cwy_fleet model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddFleet(MODEL.qgzx_cwy_fleet model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_fleetBLL.Add(model);

            if (res > 0)
            {

                return Redirect("/cwy/Information/GetFleetData");
            }
            return Redirect("/cwy/Information/GetFleetData");
        }
        #endregion

        #region 删除乘务员车队cwy_fleet信息 + DelFleet()
        /// <summary>
        /// 删除乘务员车队cwy_fleet信息 + DelFleet()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelFleet()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_cwy_fleetBLL.DelBy(x => x.FleetId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了,您现在删除的车队正在被使用" + ex, null, "");
            }
        }
        #endregion

        #region 搜索方法 --ActionResult SearchFleet()
        /// <summary>
        /// 多条件搜索
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SearchFleet()
        {
            string Introduce = Request.QueryString["Introduce"];
            string WorkPlace = Request.QueryString["WorkPlace"];
            string Leader = Request.QueryString["Leader"];
            string Volunteers = Request.QueryString["Volunteers"];
            string Passengers = Request.QueryString["Passengers"];
            string StudentName = Request.QueryString["StudentName"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！

            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_fleetBLL.GetListBy(x => x.FleetId != null).ToList().Select(x => x.ToPOCO());
            if (Introduce != "")
            {
                list = list.Where(x => x.Introduce.ToString().Contains(Introduce)).ToList();
            }
            if (WorkPlace != "")
            {
                list = list.Where(x => x.WorkPlace.Contains(WorkPlace)).ToList();
            }
            if (Leader != "")
            {
                list = list.Where(x => x.Leader.Contains(Leader)).ToList();
            }
            if (Volunteers != "")
            {
                list = list.Where(x => x.Volunteers.ToString().Contains(Volunteers)).ToList();
            }
            if (Passengers != "")
            {
                list = list.Where(x => x.Passengers.Contains(Passengers)).ToList();
            }
            if (StudentName != "")
            {
                list = list.Where(x => x.StudentName.Contains(StudentName)).ToList();
            }
       
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        //-----------------对表cwy_group车班信息表的增删改查-------------//
        #region 获取乘务员cwy_group表格数据 +GetGroupData()
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取乘务员cwy_group表格数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetGroupData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_groupBLL.GetPagedList(pageIndex, pageSize, x => x.FleetID != null && x.GroupID != null, m => m.GroupID).Select(x => x.ToPOCO());
            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 加载修改乘务员车班 cwy_group信息表的 window -- ActionResult EditGroup（）
        /// <summary>
        /// 加载修改乘务员车班 cwy_group信息表的 window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult EditGroup(int id)
        {
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_cwy_groupBLL.GetListBy(x => x.GroupID == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改乘务员车班 cwy_group信息表的 EditGroup(MODEL.qgzx_cwy_group model)
        /// <summary>
        ///post修改乘务员车班 cwy_group信息表的 EditGroup(MODEL.qgzx_cwy_group model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult EditGroup(MODEL.qgzx_cwy_group model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_groupBLL.Modify(model, "Leader", "Headman", "HeadmanId", "Volunteers", "AdvancedDeeds", "WorkDays", "VocationDays", "TimeSpan", "IsExcellent");
            if (res > 0)
            {
                return Redirect("/cwy/Information/GetGroupData");
            }
            return Redirect("/cwy/Information/GetGroupData");
        }
        #endregion

        #region 新增乘务员车班信息cwy_group的window + AddGroup()
        /// <summary>
        /// 新增乘务员车班信息cwy_group的window
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddGroup(int id)
        {
            MODEL.ViewModel.CwyGroup group = new MODEL.ViewModel.CwyGroup();
            group.FleetID = id;
            return PartialView("EditGroup", group);
        }
        #endregion

        #region 新增乘务员车班信息cwy_group的 html代码 + AddGroup(MODEL.qgzx_cwy_group model)
        /// <summary>
        /// 新增乘务员车班信息cwy_group的 html代码 + AddGroup(MODEL.qgzx_cwy_group model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddGroup(MODEL.qgzx_cwy_group model)
        {

            int res = OperateContext.Current.BLLSession.Iqgzx_cwy_groupBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/cwy/Information/GetGroupData");
            }
            return Redirect("/cwy/Information/GetGroupData");
        }
        #endregion

        #region 删除乘务员车班cwy_group信息 + DelGroup()
        /// <summary>
        /// 删除乘务员车班cwy_group信息 + DelGroup()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelGroup()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_cwy_groupBLL.DelBy(x => x.GroupID == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "出错了,您现在删除的车班正在被使用" + ex, null, "");
            }
        }
        #endregion

        #region 搜索方法 --ActionResult SearchGroup()
        /// <summary>
        /// 多条件搜索
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SearchGroup()
        {
            string Leader = Request.QueryString["Leader"];
            string Headman = Request.QueryString["Headman"];
            string AdvancedDeeds = Request.QueryString["AdvancedDeeds"];
            string WorkDays = Request.QueryString["WorkDays"];
            string TimeSpan = Request.QueryString["TimeSpan"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！

            var list = OperateContext.Current.BLLSession.Iqgzx_cwy_groupBLL.GetListBy(x => x.GroupID!= null).ToList().Select(x => x.ToPOCO());
            if (Leader != "")
            {
                list = list.Where(x => x.Leader.Contains(Leader)).ToList();
            }
            if (Headman != "")
            {
                list = list.Where(x => x.Headman.Contains(Headman)).ToList();
            }
            if (AdvancedDeeds != "")
            {
                list = list.Where(x => x.AdvancedDeeds.Contains(AdvancedDeeds)).ToList();
            }
            if (WorkDays != "")
            {
                list = list.Where(x => x.WorkDays.ToString().Contains(WorkDays)).ToList();
            }
            if (TimeSpan != "")
            {
                list = list.Where(x => x.TimeSpan.ToString().Contains(TimeSpan)).ToList();
            }

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

    }
}
